//
//  YJDHelper.m
//  YJDSDK
//
//  Created by skyline on 2018/7/25.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDHelper.h"

@interface YJDHelper()
{
    NSMutableDictionary *_localInfo;
    NSMutableArray *_accountArr;
    NSString *_account;
    BOOL _isBind;
    BOOL _isAuthenticate;
}
@end

@implementation YJDHelper
static YJDHelper*_helper;
static dispatch_once_t onceToken;
+ (instancetype)shareHelp
{
    if (!_helper)
    {
        dispatch_once(&onceToken,^{
            _helper = [[YJDHelper alloc]init];
        });
    }
    return  _helper;
}

- (NSDictionary *)localInfo
{
    if (!_localInfo)
    {
        _localInfo = [NSMutableDictionary dictionary];
        [_localInfo setValue:[YJDDevice yjd_deviceIDFV] forKey:@"imei"];//设备号<设备唯一标识符>
        [_localInfo setValue:[YJDDevice yjd_deviceIP] forKey:@"ip"];//设备公网IP//获取公网IP比较费时，做异步处理
        [_localInfo setValue:[[YJDHelper shareHelp] appID] forKey:@"appid"];//appid<暂定1>
        [_localInfo setValue:@"1"forKey:@"terminal"];//设备平台<苹果设备为1>
        [_localInfo setValue:@"" forKey:@"phone"];//添加手机字段
    }
    return _localInfo;
}

- (NSMutableArray *)accountArr
{
    if (!_accountArr)
    {
        _accountArr = [YJDCache yjd_readCache];//直接操作accountArr增删动作，自动存储，不需要再调用存储操作
    }
    return _accountArr;
}

- (NSString *)account
{
    return _account;
}

- (NSString *)accountID
{
    return [[YJDPlatform platform] accountID]?[[YJDPlatform platform] accountID]:@"";
}

- (NSString *)appID
{
    return [[YJDPlatform platform] appID];
}

- (BOOL)isBind
{
    return _isBind;
}

- (BOOL)isAuthenticate
{
    return _isAuthenticate;
}

+ (void)yjd_insertAccount:(NSDictionary *)dict
{
    _helper = [YJDHelper shareHelp];
    
    _helper->_account = [dict valueForKey:@"name"];
    NSString *password = [dict valueForKey:@"password"];
    if (_helper->_account && password)
        [_helper.accountArr yjd_enumerate:@{_helper->_account:password}];
}

+ (void)yjd_setupBindState:(NSString *)string
{
    _helper = [YJDHelper shareHelp];
    
    _helper->_isBind = ([string length] > 1)?YES:NO;
}

+ (void)yjd_setupAuthenticate:(NSString *)string
{
    _helper = [YJDHelper shareHelp];
    
    _helper->_isAuthenticate = ([string intValue] == 1)?YES:NO;
}

@end

